package org.vectrics.recruiter.struts.tag;

import java.util.Collection;
import java.util.HashSet;
import org.apache.log4j.Logger;
import org.vectrics.company.Company;
import org.vectrics.hibernate.ThreadProperties;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.user.Interview;
import org.vectrics.recruiter.user.RecruitUserProvider;
import org.vectrics.user.Person;
import org.vectrics.user.PersonPrivilegeGroupType;
import org.vectrics.user.PersonSearchCriteria;

import java.util.Set;


/**
 * Combo box which displays possible interviewers.
 */
public class InterviewerComboTag extends PeopleComboTag {
	private String interviewId = null;
	private String companyId = null;
	
	private transient static Logger log = Logger.getLogger(InterviewerComboTag.class);
	/**
	 * 
	 */
	public InterviewerComboTag() {
		super();
	}
	
	protected boolean getAddNoPersonOption() {
		return(true);
	}
	
	protected Collection findPersons() {
		PersonSearchCriteria criteria = CoreServiceLocator.createPersonSearchCriteria();
		
		
		criteria.addDomain(ThreadProperties.getDomain());
		criteria.getDomains().addAll(ThreadProperties.getDomain().retrieveChildrenInBranch());

		//if ((this.companyId == null) && (this.companyId.length() == 0)) {
		//	Company company = CoreServiceLocator.getCompanyService().findCompany(new Long(this.getCompanyId()));
		//	criteria.setCompany(company);
		//}
		PersonPrivilegeGroupType groupType = CoreServiceLocator.getPersonService().findPersonPrivilegeGroupType("hiring.mgr");
		criteria.addPersonPrivilegeGroup(groupType);
		groupType = CoreServiceLocator.getPersonService().findPersonPrivilegeGroupType("recruiter");
		criteria.addPersonPrivilegeGroup(groupType);
		groupType = CoreServiceLocator.getPersonService().findPersonPrivilegeGroupType("recruiter.manager");
		criteria.addPersonPrivilegeGroup(groupType);
		groupType = CoreServiceLocator.getPersonService().findPersonPrivilegeGroupType("hiring.manager");
		criteria.addPersonPrivilegeGroup(groupType);
		
		Collection persons = CoreServiceLocator.getPersonService().findPersons(criteria, 0, 10);
		return(persons);
	}

	public Long getSelectedPersonId() {
		log.debug("getSelectedUserId() - returning the userId of the interviewer that should be selected in the combo");
		Long interviewerId = null;
		if ((this.interviewId == null) || (interviewId.trim().length() == 0)) {
			log.debug("interview id is null, no selected user.");
		} else {
			log.debug("interview id is not null, looking up");
            Interview interview = RecruitServiceLocator.getRecruitUserService().findInterviewById(new Long(interviewId));
            if ((interview != null) && (interview.getLeadPerson() != null)) {
            	interviewerId = interview.getLeadPerson().getId();
            } else {
            	log.debug("interview not found");
            }
		}
		return interviewerId;
	}
	

	protected Set getRoles() {
		HashSet roles = new HashSet();
		
		roles.add("recruit-mgr");
		roles.add("recruit-employee");
		roles.add("recruit-recruiter");
		return(roles);
	}

	/**
	 * @return Returns the interviewId.
	 */
	public String getInterviewId() {
		return interviewId;
	}
	/**
	 * @param interviewId The interviewId to set.
	 */
	public void setInterviewId(String interviewId) {
		this.interviewId = interviewId;
	}

	/**
	 * @return Returns the companyId.
	 */
	public String getCompanyId() {
		return companyId;
	}

	/**
	 * @param companyId The companyId to set.
	 */
	public void setCompanyId(String companyId) {
		this.companyId = companyId;
	}
}
